#!/usr/bin/python
#Auther : MillionLee

'''
Update user list

Update user list. Include ops: insert, delete, update etc.
'''

#from google.appengine.ext import db

from userListModel import UserDB

def updateUser(key, secret = None, name = None, lastUpdateDateStr = None):
    userItems = UserDB.gql("WHERE _userkey = :requestKey ORDER BY _userkey DESC",
                             requestKey=key)
    if userItems.count() > 1:
        raise Exception("User requested is not unique")
        return
    userItem = userItems[0]
    userItem._userkey = key
    if secret != None:
        userItem._secret = secret
    if name != None:
        userItem._name = name
    if lastUpdateDateStr != None:
        userItem._lastUpdateDateStr = lastUpdateDateStr
    userItem.put()
    
def insertUser(key, secret, name):
    userItems = UserDB.gql("WHERE _userkey = :newKey ORDER BY _userkey DESC",
                             newKey=key)
    if userItems.count() > 0:
        raise Exception("User requested was already exist")
        return
    userItem = UserDB()
    userItem._userkey = key
    userItem._secret = secret
    userItem._name = name
    userItem._lastUpdateDateStr = ""
    userItem.put()